﻿using log4net;
using ServerControl.Dal;
using ServerControl.Model;
using ServerControl.WebManage.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServerControl.Utils;
using ServerControl.Model.Enum;
using System.Text;
using MiniExcelLibs;
using System.IO;
using System.Web.Http.Results;
using static System.Net.Mime.MediaTypeNames;
using WebGrease.Css.Extensions;
using MiniExcelLibs.OpenXml;
using System.Security.Principal;
using Newtonsoft.Json;

namespace ServerControl.WebManage.Controllers.Admin
{
    [RoutePrefix("admin/account")]
    [Auth(AllowRole = Model.Enum.Role.AdminOrUser)]
    public class AccountController : BaseController
    {
        private ILog logger = LogManager.GetLogger("AccountController");

        [Route("index")]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        [Route("list")]
        public JsonResult ListPage(int pageIndex = 1, int pageSize = 20, string nickName = "", string status = "")
        {
            try
            {
                int records = 0;
                var datas = AccountDal.ListPage(ref records, pageIndex, pageSize, CurrentUser.UserName, nickName, status);
                return new JsonNetResult { Data = new SuccessResult<List<Account>> { Data = datas, Count = records, Msg = "" } };
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }

        [HttpPost]
        [Route("delete")]
        public JsonResult Delete(List<string> uniqueIds)
        {
            try
            {
                if (AccountDal.Delete(CurrentUser.UserName, uniqueIds))
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "删除成功" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "删除失败" } };
        }

        //[HttpPost]
        //[Route("edit")]
        //public JsonResult Edit(Account account)
        //{
        //    try
        //    {
        //        account.UserName = CurrentUser.UserName;

        //        if (AccountDal.Update(account))
        //        {
        //            return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        logger.Error(ex);
        //    }

        //    return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
        //}

        [HttpPost]
        [Route("add")]
        public JsonResult Add(Account account)
        {
            try
            {
                account.UserName = CurrentUser.UserName;
                account.AddTime = DateTime.Now;

                if (AccountDal.Insert(account))
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
        }
    }
}